/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.graphic.objects;

import controller.commands.ClearSelectionCommand;
import controller.commands.DeleteCommand;
import controller.commands.SetSelectedCommand;
import controller.commands.ShrinkCommand;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;
import model.graphic.locations.AbsoluteLocation;
import view.GUI.LabelReader;
import view.GUI.graphic.GraphicView;
import view.GUI.graphic.SkinInfo;
import view.GUI.graphic.SkinReader;
import view.GUI.sidepanels.CustomArgument;

/**
 *
 * @author Ben
 */
public class GCommentNode extends GNode{
    public GCommentNode() {
        super();
        this.setValue("");
    }

    @Override
    public void setValue(String value) {
        super.setValue(value);
        this.setToolTipText(LabelReader.toHTML(value));
    }

    @Override
    public void paint(Graphics2D g) {
        if(this.isVisible()){
            SkinInfo info = SkinReader.getInstance().getSkinInfo(type);
            Color oldColor = g.getColor();
            Stroke oldStroke = g.getStroke();
            getRemoveAddon().setCenter(new AbsoluteLocation(lowerRight.getX() - 10, lowerRight.getY() - 10));
            this.drawSkin(info, g);

            if (highlighted || selected) {
                g.setColor(highlightColor);
            } else {
                g.setColor(baseColor);
            }

            g.setStroke(new BasicStroke(2));
            g.drawRect(location.getX(), location.getY(), getWidth(), getHeight());
            g.setStroke(new BasicStroke(1));

            //Draw the comment
            g.setFont(info.getFont());
            g.setColor(info.getFontColor());
            FontMetrics fm = g.getFontMetrics();
            Rectangle2D textBounds = fm.getStringBounds(this.getValue(), g);
            String[] lines = this.getValue().split("\n");
            int yDelta = (int)(textBounds.getHeight());
            int mWidth = (int)fm.getStringBounds("m", g).getWidth(); // M is the most wide character
            int i = 0;
            while( i < lines.length && (10 + i * yDelta) < this.getHeight()){
                textBounds = fm.getStringBounds(lines[i], g);
                String line = "";
                if(textBounds.getWidth() + 5 > this.getWidth()){
                    line = lines[i].substring(0,this.getWidth() / mWidth ) + "...";
                }else{
                    line = lines[i];
                }
                g.drawString(line, this.getX() +5, this.getY() + 10 + i * yDelta);
                i++;
            }
            if((10 + i * yDelta) > this.getHeight()){
                g.drawString("...", this.getX()+5, this.getY() + 10+3+ (i-1) * yDelta);
            }
            
            if (!getRemoveAddon().isAddonHighlighted()) {
                g.setStroke(oldStroke);
                g.setColor(Color.RED);
            } else {
                g.setStroke(new BasicStroke(4.0f));
                g.setColor(Color.BLUE);
            }
            g.draw(getRemoveAddon().getL1());
            g.draw(getRemoveAddon().getL2());
            g.setStroke(oldStroke);
            g.setColor(oldColor);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        final GraphicView gv = (GraphicView) this.getParent();
        if (!e.isControlDown() && notclickedAddon) {
            gv.notifyObservers(new ClearSelectionCommand());
            gv.notifyObservers(new SetSelectedCommand(this, true));
            gv.notifyObservers(new CustomArgument("tabRight", 0));
        } else {
            if (!this.isSelected() && notclickedAddon) {
                gv.notifyObservers(new SetSelectedCommand(this, true));
                gv.notifyObservers(new CustomArgument("tabRight", 0));
            }
        }
        if (e.getClickCount() == 1) {
            if (this.getRemoveAddon().inAddon(e.getX() + this.getGraphLocation().getX(), e.getY() + this.getGraphLocation().getY())) {
                gv.notifyObservers(new DeleteCommand(this));
                gv.notifyObservers(new CustomArgument("tabRight", 1));
            }
            if (this.getShrinkAddon().inAddon(e.getX() + this.getGraphLocation().getX(), e.getY() + this.getGraphLocation().getY())) {
                gv.notifyObservers(new ShrinkCommand(this));
            }
        } else if (e.getClickCount() == 2 && notclickedAddon) {
            final JLayeredPane jp = getRootPane().getLayeredPane();
            final JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.setSize(this.getWidth() - 6, this.getHeight());
            Component tab = ((JTabbedPane)this.getParent().getParent().getParent().getParent()).getSelectedComponent();
            Point delta = ((JViewport)this.getParent().getParent()).getViewPosition();
            p.setLocation((int)(gv.getRootPane().getLocation().getX() + tab.getLocation().getX() + super.getUpperLeft().getX() - delta.getX()),
                          (int)(gv.getRootPane().getLocation().getY() + tab.getLocation().getY() + super.getUpperLeft().getY() + 30 - delta.getY()));
            CommentTextArea et = new CommentTextArea(this,p);
            JScrollPane scrollPane = new JScrollPane(et);
            p.add(scrollPane);
            jp.setLayer(p, 10);
            jp.add(p);
            et.requestFocus();
        }
        gv.repaint();
    }

}



